package com.joker.repository;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RootCreatZg {
    public boolean creat(String s1, String s2, String s3){
//        username等于userid
        String name=s1;
        String xinxi=s2;
        String zhuangtai=s3;
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd ");
        Date date = new Date(System.currentTimeMillis());
//        System.out.println(formatter.format(begintime));
//        System.out.println(formatter.format(endtime));


        ResultSet resultSet=null;

        int i=0;
        Connection connection= null;
        Boolean b1=true;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/huizhan?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
            String root="root";
            String password="123456";


            try {
                connection = DriverManager.getConnection(url,root,password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            String sql="select * from zhanguan where name=? and xinxi=?";
            try {
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,xinxi);

                resultSet=preparedStatement.executeQuery();
                System.out.println("这是结果"+resultSet);
                b1=resultSet.next();
                System.out.println("输出布尔结果"+b1);
                if(!b1)
                {
                    sql="insert into zhanguan(name,xinxi,zhuangtai) values (?,?,?)";
                    preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1,name);
                    preparedStatement.setString(2,xinxi);
                    preparedStatement.setString(3,zhuangtai);
//                    preparedStatement.setDate(5, java.sql.Date.valueOf(formatter.format(t1)));
//                    preparedStatement.setDate(6, java.sql.Date.valueOf(formatter.format(t1)));

                    i=preparedStatement.executeUpdate();
                    System.out.println("输出插入后的数据返回值"+i);
                }
//                System.out.println(resultSet);
            }
            catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(b1){
            return false;
        }
        else {
            return true;
        }
    }
}
